package com.ldc.framework.activity

import android.content.Context
import android.content.Intent
import android.os.Message
import android.view.View
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.uts.ApiUtils
import com.ldc.commonlibs.uts.MessageUts
import com.ldc.commonlibs.uts.ViewModelUtils
import com.ldc.commonres.common.VBMActivity
import com.ldc.commonres.views.StateViewLayout
import com.ldc.framework.R
import com.ldc.framework.adapter.RankAdapter
import com.ldc.framework.databinding.FrameworkActivityRankBinding
import com.ldc.framework.viewmodel.FrameworkViewModelBase
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class RankActivity : VBMActivity<FrameworkActivityRankBinding, FrameworkViewModelBase>() {
    private var rankAdapter: RankAdapter? = null
    private var page: Int = 0

    companion object {
        private const val ARG_TITLE = "title"
        private const val REFRESH_CODE = 0x1313
        fun start(context: Context, title: String) {
            context.startActivity(Intent(context, RankActivity::class.java).apply {
                putExtra(ARG_TITLE, title)
            })
        }

        fun start(fragment: Fragment, title: String) {
            start(fragment.requireContext(), title)
        }
    }

    override fun processMessage(msg: Message): Boolean {
        when (msg.what) {
            REFRESH_CODE -> {
                getRankList()
            }
        }
        return super.processMessage(msg)
    }

    override fun initStateViewLayout(): StateViewLayout? =
        StateViewLayout.Companion.init(this, R.id.content_view)

    override fun main() {
        addOnReTryListener(object : OnRowListener<Int> {
            override fun onRow(v: View, data: Int) {
                page = 0
                MessageUts.sendMessage(getHandler(), REFRESH_CODE)
            }

        })
        initAdapter()
        viewBind?.topBar?.let {
            it.flBack.setOnClickListener { finish() }
            it.tvName.text = intent?.run {
                getStringExtra(ARG_TITLE)
            }
        }
        MessageUts.sendMessage(getHandler(), REFRESH_CODE)

    }

    override fun createViewBinding(): FrameworkActivityRankBinding =
        FrameworkActivityRankBinding.inflate(layoutInflater)

    override fun createViewModel(): FrameworkViewModelBase =
        ViewModelUtils.create(this, FrameworkViewModelBase::class.java)


    // 初始化适配器
    private fun initAdapter() {
        rankAdapter = RankAdapter()
        viewBind?.contentView?.let { cv ->
            cv.rv.recyclerview.let { rv ->
                rv.adapter = rankAdapter
                rv.layoutManager =
                    LinearLayoutManager(rv.context, LinearLayoutManager.VERTICAL, false)
                rv.setHasFixedSize(true)
            }
            //
            cv.refreshView.setOnRefreshLoadMoreListener(object :
                OnRefreshLoadMoreListener {
                override fun onRefresh(refreshLayout: RefreshLayout) {
                    page = 0
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }

                override fun onLoadMore(refreshLayout: RefreshLayout) {
                    page += 1
                    MessageUts.sendMessage(getHandler(), REFRESH_CODE)
                }
            })
        }
    }


    private fun getRankList() {
        showLoadingView()
        viewModel?.rankList(page)?.observe(this) {
            showContentView()
            viewBind?.contentView?.refreshView?.also { rv ->
                if (rv.state.isOpening) {
                    rv.finishLoadMore()
                    rv.finishRefresh()
                }
            }

            if (ApiUtils.isSuccess(it)) {
                if (page == 0) {
                    rankAdapter?.newData(it.data?.datas)
                } else {
                    rankAdapter?.addNewData(it.data?.datas)
                }
                viewBind?.contentView?.refreshView?.setNoMoreData(page == it.data?.pageCount)
            }
            if (rankAdapter?.isEmptyData()!!) {
                showEmptyView()
            }
        }

    }


}